package by.bsu.fpmi.bbtp;

import by.bsu.fpmi.bbtp.authentication.LoginController;
import by.bsu.fpmi.bbtp.authentication.Role;
import by.bsu.fpmi.bbtp.controllers.ClientController;
import by.bsu.fpmi.bbtp.controllers.GoodController;
import by.bsu.fpmi.bbtp.controllers.TransportCompanyController;
import by.bsu.fpmi.bbtp.dao.ClientDAO;
import by.bsu.fpmi.bbtp.dao.GoodDAO;
import by.bsu.fpmi.bbtp.dao.OrderDAO;
import by.bsu.fpmi.bbtp.dao.TransportCompanyDAO;
import by.bsu.fpmi.bbtp.dao.UserDAO;
import by.bsu.fpmi.bbtp.entities.Order;
import by.bsu.fpmi.bbtp.entities.TransportCompany;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.List;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/application-context.xml")
public class Sandbox {
    
    private static final String NAME = "username";
    private static final String PASSWORD = "password";
    private static final Role ROLE = Role.SALESMAN;
    @Autowired
    private UserDAO userDao;
    @Autowired
    private TransportCompanyDAO transportCompanyDao;
    @Autowired
    private GoodDAO goodDao;
    @Autowired
    private OrderDAO orderDao;
    @Autowired
    private ClientDAO clientDao;
    @Autowired
    private LoginController loginController;
    @Autowired
    private TransportCompanyController transportCompanyController;
    @Autowired
    private GoodController goodController;
    @Autowired
    private ClientController clientController;

    private static void log(Object obj) {
        System.out.println(obj.toString());
    }


    @Test
    public void runSandbox() throws Exception {
        log("\n");
        log(clientController.clientExists("42", "ivanoV@email.com"));
        log("\n");
    }

    @Test
    public void showAll() {
        showAllUsers();
        showAllCompanies();
        showAllGoods();
        showAllClients();
        showAllOrders();
    }

    @Test
    public void showAllClients() {
        show("clients", clientDao.all());
    }

    @Test
    public void showAllCompanies() {
        show("transport companies", transportCompanyDao.all());
    }

    @Test
    public void showAllUsers() {
        show("users", userDao.all());
    }
    
    @Test
    public void showAllGoods() {
        show("goods", goodDao.all());
    }



    @Test
    public void showAllOrders() {
        log("\n\n");
        log("All orders:");
        for (Order order : orderDao.all()) {
            log(orderToString(order) + "\n");
        }
        log("\n");
    }
    
    private void show(String name, List<?> objects) {
        log("All " + name);
        for (Object o : objects) {
            log(o);
        }
        log("\n");
    }
    
    private String orderToString(Order order) {
        return String.format("{client=%s, type=%s, state=%s, item=%s, quantity=%d, address=%s}",
                clientDao.find(order.getClientId()).toString(),
                order.getType(), 
                order.getState(),
                goodDao.find(order.getGoodId()).getName(),
                order.getQuantity(),
                order.getAddress());
    }
    
    
}

